package com.weavernorth.retailExperienceAward.service;

import com.weavernorth.util.Logger4jUtil;
import com.weavernorth.util.dateUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.workflow.webservices.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: bruseewang
 * @Package: com.weavernorth.retailExperienceAward.service
 * @date: 2020/6/2 16:47
 * @description: 定时任务：店铺零售体验考核申请流程-区域经理-只取A/A+店铺对应的区域经理
 */
public class StoreRetailExpQyjlService {
//    private static Log log = LogFactory.getLog(StoreRetailExpQyjlService.class);
    public Logger log;
    public Integer cycle;
    public Logger getLog() {
        return log;
    }
    public void setLog(Logger log) {
        this.log = log;
    }
    public Integer getcycle() {
        return cycle;
    }
    public void setcycle(Integer cycle) {
        this.cycle = cycle;
    }
    public void createFormal() {
        RecordSet rs_detail = new RecordSet();
        RecordSet rs_rygw = new RecordSet();
        //基础类
        BaseBean bb = new BaseBean();
        String strCreator = "1";//31299:周艳梅
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String strJcrq = df.format(new Date());
        String strKhzq= dateUtils.previousMonth(0);
        if (cycle!=null){
            strKhzq=dateUtils.previousMonth(cycle);
        }
        log.info("考核周期为："+strKhzq);
        //创建流程的主表
        //存放主表字段的数组
        WorkflowRequestTableField[] wrMain = new WorkflowRequestTableField[3];
        //创建人
        log.info("创建人字段赋值：");
        wrMain[0] = new WorkflowRequestTableField();
        wrMain[0].setFieldName("sqr");
        wrMain[0].setFieldValue(strCreator);
        wrMain[0].setView(true);
        wrMain[0].setEdit(true);
        wrMain[0].setFieldOrder(0);
        wrMain[0].setMand(false);

        //创建时间
        log.info("考核周期：");
        wrMain[1] = new WorkflowRequestTableField();
        wrMain[1].setFieldName("khzq");
        wrMain[1].setFieldValue(strKhzq);
        wrMain[1].setView(true);
        wrMain[1].setEdit(true);
        wrMain[1].setFieldOrder(0);
        wrMain[1].setMand(false);
        //创建时间
        log.info("创建时间：");
        wrMain[2] = new WorkflowRequestTableField();
        wrMain[2].setFieldName("cjsj");
        wrMain[2].setFieldValue(strJcrq);
        wrMain[2].setView(true);
        wrMain[2].setEdit(true);
        wrMain[2].setFieldOrder(0);
        wrMain[2].setMand(false);
        //填装主表信息
        WorkflowRequestTableRecord[] wrtri = new WorkflowRequestTableRecord[1];
        wrtri[0] = new WorkflowRequestTableRecord();
        wrtri[0].setWorkflowRequestTableFields(wrMain);
        WorkflowMainTableInfo wmi = new WorkflowMainTableInfo();
        wmi.setRequestRecords(wrtri);
        //获取主表,添加配置文件，配置流程id
        String strTable = bb.getPropValue("retailExperience", "qyjlTable");
        log.info("流程表名：" + strTable);
        //明细表
        JSONArray jsonArray = new JSONArray();
        //创建视图：v_uf_dpxx_fgs：SELECT fgs,qyjl FROM uf_dpxx WHERE fgs IS NOT NULL AND qyjl IS NOT NULL GROUP BY fgs,qyjl
        String strSqls = "SELECT a.fgs,a.qyjl,b.workcode,b.jobtitle as gw FROM v_uf_dpxx_fgs a,hrmresource b,uf_cfdplstykh c  where a.FGS=c.fgsppdy and a.qyjl=b.id "+
                " AND NOT EXISTS (SELECT 1 FROM "+strTable+" main,"+strTable+"_dt1 detail WHERE a.FGS=detail.fgs AND main.khzq='"+strKhzq+"') ";
        rs_detail.executeQuery(strSqls);
        log.info("区域经理店铺信息-遍历明细信息SQL：" + strSqls);
        try {
            while (rs_detail.next()) {
                JSONObject jsonObject = new JSONObject();
                String strfgs = Util.null2String(rs_detail.getString("fgs"));//分公司
                jsonObject.put("fgs", strfgs);
                String strXm = Util.null2String(rs_detail.getString("qyjl"));//姓名
                jsonObject.put("xm", strXm);
                String strWorkCode = Util.null2String(rs_detail.getString("workcode"));//人员编号
                jsonObject.put("gh", strWorkCode);
                String strGw = Util.null2String(rs_detail.getString("gw"));
                jsonObject.put("zw", strGw);
                jsonArray.add(jsonObject);

            }
        }catch (Exception ex){
            log.info("程序执行异常,异常信息："+ex.toString());
        }

        log.info("读取属性文件,配置workflowID开始。。。：");
        //基本信息
        WorkflowBaseInfo wbi = new WorkflowBaseInfo();
        //获取workflowid,添加配置文件，配置流程id
        String workflowid = bb.getPropValue("retailExperience", "retailQj_FlowId");
        log.info("读取属性文件,配置workflowID结束。。。；workflowId=："+workflowid);
        wbi.setWorkflowId(workflowid);

        WorkflowRequestInfo wrInfo = new WorkflowRequestInfo();
        wrInfo.setCreatorId("01");
        wrInfo.setRequestLevel("0");//紧急程度
        wrInfo.setRequestName("品牌-区域经理零售体验奖考核申请流程");//请求的名称
        //停留在提交节点
        wrInfo.setIsnextflow("1");
        //主表数据
        wrInfo.setWorkflowMainTableInfo(wmi);
        wrInfo.setWorkflowBaseInfo(wbi);

        //明细表
        WorkflowDetailTableInfo wdti[] = new WorkflowDetailTableInfo[1];
        //限定明细表的长度
        wrtri = new WorkflowRequestTableRecord[jsonArray.size()];
        wdti = getDetail(jsonArray, wdti, wrtri,strKhzq);
        wrInfo.setWorkflowDetailTableInfos(wdti);
        wrInfo.setCreatorId(strCreator);

        WorkflowServiceImpl wfsi = new WorkflowServiceImpl();
        String requestid = wfsi.doCreateWorkflowRequest(wrInfo, Integer.parseInt(strCreator));
        log.info("区域经理零售体验奖考核申请流程-批量生成流程返回值==" + requestid);

        if (!"".equals(requestid) && Integer.parseInt(requestid) > 0) {
            log.info("流程创建成功！");
        } else {
            log.info("流程创建失败！");
        }


    }

    /**
     * 封装明细数据
     */

    public WorkflowDetailTableInfo[] getDetail(JSONArray jsonArray, WorkflowDetailTableInfo[] wdti, WorkflowRequestTableRecord[] wrtri,String strKhzq) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String strJcrq = df.format(new Date());
        for (int i = 0; i < jsonArray.size(); i++) {

            JSONObject jsonObject = jsonArray.getJSONObject(i);
            log.info("封装明细json:" + jsonObject);
            try {
                WorkflowRequestTableField[] wrti = new WorkflowRequestTableField[13];
                wrti[0] = new WorkflowRequestTableField();
                wrti[0].setFieldName("fgs");
                wrti[0].setFieldValue(jsonObject.getString("fgs"));
                wrti[0].setView(true);
                wrti[0].setEdit(true);

                wrti[1] = new WorkflowRequestTableField();
                wrti[1].setFieldName("xm");
                wrti[1].setFieldValue(jsonObject.getString("xm"));
                wrti[1].setView(true);
                wrti[1].setEdit(true);

                wrti[2] = new WorkflowRequestTableField();
                wrti[2].setFieldName("xmdr");
                wrti[2].setFieldValue(jsonObject.getString("xm"));
                wrti[2].setView(true);
                wrti[2].setEdit(true);

                wrti[3] = new WorkflowRequestTableField();
                wrti[3].setFieldName("gh");
                wrti[3].setFieldValue(jsonObject.getString("gh"));
                wrti[3].setView(true);
                wrti[3].setEdit(true);

                wrti[4] = new WorkflowRequestTableField();
                wrti[4].setFieldName("ghdr");
                wrti[4].setFieldValue(jsonObject.getString("gh"));
                wrti[4].setView(true);
                wrti[4].setEdit(true);

                wrti[5] = new WorkflowRequestTableField();
                wrti[5].setFieldName("zw");
                wrti[5].setFieldValue(jsonObject.getString("zw"));
                wrti[5].setView(true);
                wrti[5].setEdit(true);

                wrti[6] = new WorkflowRequestTableField();
                wrti[6].setFieldName("fwbz40");
                wrti[6].setView(true);
                wrti[6].setEdit(true);

                wrti[7] = new WorkflowRequestTableField();
                wrti[7].setFieldName("clbz20");
                wrti[7].setView(true);
                wrti[7].setEdit(true);

                wrti[8] = new WorkflowRequestTableField();
                wrti[8].setFieldName("yybz35");
                wrti[8].setView(true);
                wrti[8].setEdit(true);

                wrti[9] = new WorkflowRequestTableField();
                wrti[9].setFieldName("crm5");
                wrti[9].setView(true);
                wrti[9].setEdit(true);

                wrti[10] = new WorkflowRequestTableField();
                wrti[10].setFieldName("zdf");
                wrti[10].setView(true);
                wrti[10].setEdit(true);

                wrti[11] = new WorkflowRequestTableField();
                wrti[11].setFieldName("khrq");
                wrti[11].setFieldValue(strJcrq);
                wrti[11].setView(true);
                wrti[11].setEdit(true);

                wrti[12] = new WorkflowRequestTableField();
                wrti[12].setFieldName("khzq");
                wrti[12].setFieldValue(strKhzq);
                wrti[12].setView(true);
                wrti[12].setEdit(true);

                wrtri[i] = new WorkflowRequestTableRecord();
                wrtri[i].setWorkflowRequestTableFields(wrti);
            } catch (Exception ex) {
                log.info("封装明细表数据出现异常,异常信息：" + ex.toString());
            }


        }
        wdti[0] = new WorkflowDetailTableInfo();
        wdti[0].setWorkflowRequestTableRecords(wrtri);
        return wdti;

    }

}
